function fuyilist(id) {
    $.get(baseURL + "sys/tiananfuyi/lists/?tianid=" + id, function (r) {

        var html = "<tr style='background: #ddd;'><td style='width: 140px; height: 30px;text-align: center; border: 1px solid #ddd;'>姓名</td><td style='width: 140px;height: 30px; border: 1px solid #ddd;'>状态</td></th>";
        for (var i = 0; i < r.lists.length; i++) {
            html = html+  '<tr><td style=" height: 25px; border: 1px solid #ddd;">' + r.lists[i].username + '</td><td style=" height: 25px;  border: 1px solid #ddd;">' + status(r.lists[i].status) + '</td></tr>'
        }
        function status(index) {
            if (index == '1') {
                return "同意"
            }
            if (index == '2') {
                return "不同意"
            }
            if (index == '0') {
                return "待处理"
            }
        }

        layer.open({
            title: "附议查看",
            shade: 0.5,
            type: 1,
            skin: 'layui-layer-molv',
            area: ['300px', '300px'],
            content: '    <div id="jqGridtables" style="padding: 10px;">' +
                '        <table id="jqGrids" style="text-align: center;">'+html+'</table>' +

                '    </div>',
            success: function (layero, index) {






            }
        })
    });

}

function pingjia(id) {
    $.get(baseURL + "sys/tianan/info/" + id, function (r) {
        vm.tianan = r.tianan;
        layer.open({
            title: vm.tianan.title,
            shade: 0.5,
            type: 1,
            skin: 'layui-layer-molv',
            area: ['800px', '450px'],
            btn: ['评价'],
            content: '<form id="test"><div id="test1" style="padding:10px 10px  0 10px">' +
                '<div class="form-group  " style="text-align: center;" > 案由分析</div>' +
                '<div class="form-group wenzi anyoufenxi" style="margin-top: 10px;border: 1px #999 solid;"></div>' +
                '<div class="form-group " style="text-align: center;" > 措施建议</div>' +
                '<div class="form-group wenzi  cuoshijianyi" style="margin-top: 10px;border: 1px #999 solid;"></div>' +
                '<div class="form-group  user" style="line-height: 25px"></div>' +
                '<div class="form-group times " style=" "></div>' +
                '<div class="form-group " style="text-align: left;" > 答复内容：</div>' +
                '<div class="form-group wenzi dafu" style="margin-top: 5px;border: 1px #999 solid; "></div>' +
                '<div class="input-group "><label id="fileLable">相关附件：</label>' +
                '<ul id="ulli" style="border: 1px #999 solid; padding: 10px;"></ul></div>' +
                '<textarea  style="margin: 5px 0; display: none;" name=""  placeholder="请输入不满意原因" class="layui-textarea  liancause"></textarea>' +
                '<div class="form-group " style="line-height: 35px; margin-bottom: 0">' +
                '      <input type="radio" class="check" checked style="margin: 0 10px" name="lian" value="1" title="满意">满意' +
                '      <input type="radio" class="check" style="margin: 0 10px" name="lian" value="2" title="基本满意" >基本满意' +
                '      <input type="radio" class="check" style="margin: 0 10px" name="lian" value="3" title="不满意" >不满意</div>' +

                '</div></form>',
            success: function (layero, index) {
                //评分组件代码
                $(".dafu").html("<p>" + vm.tianan.subexplain + "</p>");
                $(".user").html("办理部门：" + vm.tianan.liandeptname + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;经办人：" + vm.tianan.jingbanusername);

                //评分组件代码
                $(".anyoufenxi").html("<p>" + vm.tianan.anyoufenxi + "</p>");
                $(".cuoshijianyi").html("<p>" + vm.tianan.cuoshijianyi + "</p>");
                $(".times").html("<p>部门负责人审核时间：" + vm.tianan.restime + " &nbsp;院领导审核时间：" + vm.tianan.ledtime + " </p>");

                $(".check").click(function () {
                    if ($(this).val() == 3) {
                        $(".liancause").show();
                    } else {
                        $(".liancause").hide();
                    }
                })
                if (vm.tianan.subfile) {
                    var s = [];
                    s = vm.tianan.subfile.split(",");
                    //遍历所有文件
                    for (var i = 0; i < s.length; i++) {
                        if (s[i] != null && s[i] != 'null') {
                            s[i].replace("/", "\\")
                        }
                        var fileNameIndex = s[i].lastIndexOf("\\");
                        var filePathName = s[i].substring(fileNameIndex + 1);
                        var html = filePathName;
                        downUrl.push(s[i]);

                        html = '<li>' +
                            html + '<a class="operation_type" style="color: #0c74c4; text-decoration:none" href="../../../sys/tghfile/fileDown?fileDir=' + encodeURIComponent(s[i]) + '"  > &nbsp;&nbsp;下载</a>'
                            + "<a class='operation_type'  style='color: #0c74c4;  border:none;text-decoration:none'   onclick='eye( \"" + s[i].replace(/\\/g, "//") + "\")' href='javascript:;'  > &nbsp;&nbsp;预览</a></li>";
                        $("#ulli").append(html);
                    }
                } else {
                    $("#ulli").append("<p>暂无</p>");
                }

            },
            //确认回调
            btn1: function () {
                vm.tianan.bumanyi = $(".liancause").val();
                vm.tianan.banjiestatus = '6';
                vm.tianan.manyi = $(".check[type='radio']:checked").val();
                var url = "sys/tianan/updateyushen";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.tianan),
                    success: function (r) {
                        if (r.code === 0) {
                            layer.msg("提交成功", {icon: 1});
                            vm.reload();
                        } else {
                            layer.alert(r.msg);

                        }
                    }
                });
                layer.closeAll()
            },


        });
    });

}

var curFiles = [];
var downUrl = [];
var title;

function chexiao(id) {
    $.get(baseURL + "sys/tianan/info/" + id, function (r) {
        vm.tianan = r.tianan;
        vm.tianan.delFlag = "1";
        var url = "sys/tianan/chexiao";
        $.ajax({
            type: "POST",
            url: baseURL + url,
            contentType: "application/json",
            data: JSON.stringify(vm.tianan),
            success: function (r) {
                if (r.code === 0) {
                    layer.msg("撤销成功", {icon: 1});
                    vm.reload();
                } else {
                    layer.alert(r.msg);

                }
            }
        });
    });

}

$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/tianan/list?creatuser=' + getuser().userId,
        datatype: "json",
        colModel: [
            {label: '提案名称', name: 'title', index: 'title', sortable: true, width: 60},

            {label: '部门', align: 'center', sortable: true, name: 'deptIdName', index: 'dept_id', width: 40},
            {
                label: '提案类型',
                align: 'center',
                name: 'type',
                sortable: true,
                index: 'type',
                width: 40,
                formatter: function (value, row, rowObject) {
                    return tianleibie(value);
                }
            },
            {label: '发表时间', name: 'createTime', align: 'center', sortable: true, index: 'createTime', width: 40},
            {
                label: '办理状态',
                name: 'banjiestatus',
                align: 'center',
                index: 'banjiestatus',
                width: 40, formatter: function (value, row, rowObject) {

                    if (rowObject.delFlag == 1) {
                        return "<i class='glyphicon glyphicon-time' style='color:#FF5722' aria-hidden='true'>已撤销</i>";
                    }

                    if (rowObject.type == 1 && rowObject.yushenstatus == -1) {
                        return fuyistatus(rowObject.fuyistatus)
                    }
                    if (rowObject.type == 2 && rowObject.yushenstatus == -1) {
                        return lianmingtatus(rowObject.lianmingstatus)
                    } else if (rowObject.lianstatus != 2) {
                        return lianstatus(rowObject.lianstatus);
                    } else {
                        return checkstatus(rowObject.banjiestatus);
                    }
                }
            },
            {
                label: '操作',
                name: 'id',
                index: 'id',
                align: 'center',
                width: 60,
                formatter: function (value, row, rowObject) {
                    var but = "";
                    but += '<button type="button" class="layui-btn layui-btn-xs layui-btn-normal" onclick="info(' + value + ')">详情</button>';

                    if (rowObject.type == 1) {
                        but += '<button type="button" class="layui-btn layui-btn-xs layui-btn-normal" onclick="fuyilist(' + value + ')">附议查看</button>';
                    }
                    if (rowObject.banjiestatus == 5) {
                        but += '<button type="button" class="layui-btn layui-btn-xs layui-btn-normal" onclick="pingjia(' + value + ')">评价</button>';
                    }else {

                    }
                    if (rowObject.banjiestatus == 1) {
                        but += '<button type="button" class="layui-btn layui-btn-danger layui-btn-xs layui-btn-normal" onclick="vm.update(' + value + ')">修改</button>';
                        but += '<button type="button" class="layui-btn layui-btn-danger layui-btn-xs layui-btn-normal" onclick="vm.del(' + value + ')">删除</button>';
                        if (rowObject.delFlag == 0) {
                            but += ' <button type="button" class="layui-btn layui-btn-danger layui-btn-xs layui-btn-normal" onclick="chexiao(' + value + ')">撤销</button>';
                        }
                    }
                    return but;
                }
            }

        ],
        viewrecords: true,
        height: 360,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});


        }
    });
    vm.initselect();

});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        user: getuser(),
        dictlist: getDict('tiantype'),
        userlist: getuserlisttype(0),
        deptlist: getDeptAlls(''),
        tianan: {
            id: '',
            type: '1',
            types: '',
            title: '',
            fuyiren: '',
            lianming: '',
            deptId: '',
            anyoufenxi: '',
            cuoshijianyi: '',
        },
        editor: '',
        editor1: '',
        demo1: '',
    },
    methods: {
        init: function () {
            var E = window.wangEditor;
            vm.editor = new E('#anyoufenxi');
            vm.editor1 = new E('#zhenggaicuoshi');
            // 或者 const editor = new E( document.getElementById('div1') )
            vm.editor.config.menus = [];
            vm.editor1.config.menus = [];
            vm.editor.create();
            vm.editor1.create();
            $(".qiehuan").click(function () {

                let type = this.getAttribute('value');
                vm.tianan.type = type;
                vm.show(type);
            });
            if (vm.user.iszuzhang != 1) {
                $("#type3").hide();
            }
        },
        show: function (type) {
            $(".activebut").removeClass("activebut");
            $("#type" + type + "").addClass("activebut");
            if (type === '1') {
                $('#fyr').show();
                $('#lmr').hide();
                $('#bm').hide();
            }
            if (type === '2') {
                $('#fyr').hide();
                $('#lmr').show();
                $('#bm').hide();
            }
            if (type === '3') {
                $('#fyr').hide();
                $('#lmr').hide();
                $('#bm').show();
            }
        },
        initselect: function () {
            var userlist = getuserlisttype(0);

            vm.demo1 = xmSelect.render({
                el: '#demo1',
                toolbar: {
                    show: true,
                },
                theme: {
                    color: '#0081ff',
                },
                filterable: true,
                paging: true,
                pageSize: 100,
                data: []
            });

            var user = [];
            for (var i = 0; i < userlist.length; i++) {
                var data = {name: userlist[i].realname, value: userlist[i].userId};
                user.push(data)
            }
            vm.demo1.update({
                data: user,
                autoRow: true,
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.demo1.setValue([]);
            vm.editor1.txt.html("");
            vm.editor.txt.html("");
            vm.tianan = {
                type: '1',
                types: '',
                title: '',
                fuyiren: '',
                lianming: '',
                deptId: '',
                anyoufenxi: '',
                cuoshijianyi: '',
            };
            vm.show(vm.tianan.type);
        },
        update: function (ids) {
            var id = ids != null ? ids : getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            vm.tianan.lianmingstatus = null;
            vm.tianan.fuyistatus = null;
            vm.tianan.fuyiren = vm.demo1.getValue("value");
            // vm.tianan.deptId=$("#deptId :selected").val()
            // vm.tianan.lianming=$("#lianming :selected").val()
            // vm.tianan.types=$("#types :selected").val()
            vm.tianan.cuoshijianyi = vm.editor1.txt.html();
            vm.tianan.anyoufenxi = vm.editor.txt.html();

            if (vm.tianan.type == '1') {

                if (vm.tianan.fuyiren != '' && vm.tianan.fuyiren.length < 2) {
                    layer.alert("附议人至少两名", {icon: 2});
                    return;
                } else {
                    if (vm.tianan.fuyiren.length == 0 || vm.tianan.fuyiren == null || vm.tianan.fuyiren == true || vm.tianan.fuyiren == '') {
                        vm.tianan.yushenstatus = 0;
                        vm.tianan.fuyiren = "";
                    } else {
                        vm.tianan.fuyiren = vm.tianan.fuyiren.join();
                        vm.tianan.fuyistatus = 0;
                        vm.tianan.yushenstatus = -1;
                    }
                }
            }
            if (vm.tianan.type == 2) {
                vm.tianan.lianmingstatus = 0;
                if (vm.tianan.lianming.length == 0 || vm.tianan.lianming == null || vm.tianan.lianming == '') {
                    layer.alert("联名人为必选项", {icon: 2});
                    return;
                } else {
                    vm.tianan.yushenstatus = -1;
                    vm.tianan.fuyiren = "";
                }
            }
            if (vm.tianan.type == 3) {
                vm.tianan.yushenstatus = 0;
            }

            if (nulls(vm.tianan.cuoshijianyi) || nulls(vm.tianan.anyoufenxi) || nulls(vm.tianan.title) || nulls(vm.tianan.types)) {
                layer.alert("提交失败,数据不完整");
                return;
            }
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function () {


                var url = vm.tianan.id == null ? "sys/tianan/save" : "sys/tianan/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.tianan),
                    success: function (r) {
                        if (r.code === 0) {
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        } else {
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });


        },
        del: function (event) {
            var ids = [event];
            if (ids == null) {
                return;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/tianan/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/tianan/info/" + id, function (r) {
                vm.tianan = r.tianan;
                vm.demo1.setValue(vm.tianan.fuyiren.split(","));
                vm.editor1.txt.html(vm.tianan.cuoshijianyi);
                vm.editor.txt.html(vm.tianan.anyoufenxi);
                vm.show(vm.tianan.type);
                $('#types').selectpicker('val', vm.tianan.types);
                $('#deptId').selectpicker('val', vm.tianan.deptId);
                $('#lianming').selectpicker('val', vm.tianan.lianming);
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});
vm.init();

